package networkSimulation.statistic

import org.jfree.chart.ChartFactory
import org.jfree.data.xy.{XYSeriesCollection, XYSeries}

import org.jfree.chart.ChartUtilities
import org.jfree.chart.plot.PlotOrientation
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

import java.io.File

/**
 * Visual representation of statistics.
 *
 * @author  Tim Caljé
 *          Jeroen De Raedt
 *          Jorick Van Aken
 */
class VisualStatistics{


  var counter = 0;

  /*
  *  Creates a pie diagram
  */
    def createPie(title:String,a : List[Int], as : List[String],path: String){
        val pieDataset = new DefaultPieDataset()
        for (i <- 0 until a.size){
          pieDataset.setValue(as(i), a(i))
        }
        val chart = ChartFactory.createPieChart3D(
               title,
                pieDataset,
                true,
                true,
                false);

         ChartUtilities.saveChartAsJPEG(new File(path + counter +".gif"), chart, 1000,
                500);
      counter+=1
    }



  /*
  *  Creates a XY diagram
  */
  def createXY(title:String,b : List[Int], y : String,path:String){
    val series = new XYSeries(y)
    val range = 0.until(b.length)
    var i=0
    var a = List[Int]()
    val x = "Runs"
    for (j <- 0 until b.size+1){
      a::=j
    }
    series.add(0,0)
    for (i <- range) {
         series.add(a(i),b(i))
    }

    val dataset = new XYSeriesCollection()
    dataset.addSeries(series)
    //         Generate the graph
     val chart = ChartFactory.createXYAreaChart(title, // Title
           x, // x-axis Label
           "amount", // y-axis Label
           dataset, // Dataset
           PlotOrientation.VERTICAL, // Plot Orientation
           true, // Show Legend
           true,
           false)
           ChartUtilities.saveChartAsJPEG(new File(path + counter +".gif"), chart, 1000,
                500)
      counter+=1
  }

  /*
  *  Creates a bar diagram
  */
  def createBarChart (title:String,theme:String,a : List[Int],b : List[String], actor : String,path:String){
        val dataset = new DefaultCategoryDataset()
        for (i <- 0 until a.size){
          dataset.setValue(a(i), actor, b(i))
        }
        val chart = ChartFactory.createBarChart(title,
                actor, theme, dataset, PlotOrientation.VERTICAL, false,
                true, false)
            ChartUtilities.saveChartAsJPEG(new File((path + counter +".gif")), chart, 1000,
                500)
            counter+=1
  }
  
  /*
  *  Creates a 3d line diagram
  */
  def createLineChart3D(title:String,b : List[List[Int]],z:List[String],path:String) {

    val x = "Runs"
    var count=0

    val dataset = new XYSeriesCollection()
    var a = List[Int]()
    for (j <- 0 until b(0).size+1){
      a::=j
    }

    for(list: List[Int] <- b){

      val series = new XYSeries(z(count))
      series.add(0,0)
      for (i <- 0.until(list.length)) {
         series.add(a(i),list(i))
      }
      count+=1
      dataset.addSeries(series)
    }
        val chart = ChartFactory.createXYLineChart(
            title,      // chart title
            "Runs",                      // x axis label
            "Nodes",                      // y axis label
            dataset,                  // data
            PlotOrientation.VERTICAL,
            true,                     // include legend
            true,                     // tooltips
            false                     // urls
        )
        ChartUtilities.saveChartAsJPEG(new File((path + counter +".gif")), chart, 1000,
                500)
        counter+=1
  }
}